//
//  Card.m
//  
//
//  Created by barak on 10/27/12.
//  Copyright (c) 2012 yaniv. All rights reserved.
//

#import "Card.h"

#import "CardCreditOptions.h"

@implementation Card

@synthesize creditOptions;
@synthesize deprecateData;
@synthesize holder;
@synthesize cardId;
@synthesize issuer;
@synthesize linkAccount;
@synthesize mode;
@synthesize number;
@synthesize securityCode;

+ (Card *)instanceFromDictionary:(NSDictionary *)aDictionary {

    Card *instance = [[Card alloc] init];
    [instance setAttributesFromDictionary:aDictionary];
    return instance;

}

- (void)setAttributesFromDictionary:(NSDictionary *)aDictionary {

    if (![aDictionary isKindOfClass:[NSDictionary class]]) {
        return;
    }

    [self setValuesForKeysWithDictionary:aDictionary];

}

- (void)setValue:(id)value forKey:(NSString *)key {

    if ([key isEqualToString:@"creditOptions"]) {

        if ([value isKindOfClass:[NSDictionary class]]) {
            self.creditOptions = [CardCreditOptions instanceFromDictionary:value];
        }

    } else {
        [super setValue:value forKey:key];
    }

}


- (void)setValue:(id)value forUndefinedKey:(NSString *)key {

    if ([key isEqualToString:@"id"]) {
        [self setValue:value forKey:@"cardId"];
    } else {
        [super setValue:value forUndefinedKey:key];
    }

}


- (NSDictionary *)dictionaryRepresentation {

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

    if (self.creditOptions) {
        [dictionary setObject:self.creditOptions forKey:@"creditOptions"];
    }

    if (self.deprecateData) {
        [dictionary setObject:self.deprecateData forKey:@"deprecateData"];
    }

    if (self.holder) {
        [dictionary setObject:self.holder forKey:@"holder"];
    }

    if (self.cardId) {
        [dictionary setObject:self.cardId forKey:@"cardId"];
    }

    if (self.issuer) {
        [dictionary setObject:self.issuer forKey:@"issuer"];
    }

    if (self.linkAccount) {
        [dictionary setObject:self.linkAccount forKey:@"linkAccount"];
    }

    if (self.mode) {
        [dictionary setObject:self.mode forKey:@"mode"];
    }

    if (self.number) {
        [dictionary setObject:self.number forKey:@"number"];
    }

    if (self.securityCode) {
        [dictionary setObject:self.securityCode forKey:@"securityCode"];
    }

    return dictionary;

}

@end
